<?php

namespace App\Http\Controllers;

use App\Models\ActivityProjectVideo;
use App\Models\Maker;
use App\Models\MyMessage;
use App\Models\Project;
use App\Models\Topic;
use App\Models\TopicPhoto;
use App\Models\TopicPost;
use App\Models\TopicPostPhoto;
use App\Models\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User as AUser;
use App\Http\Requests\TopicPostRequest;
class TopicPostController extends CommonController
{
    /**
     * 获取单条专题图片管理界面
     */
    public function getIndex(Request $request)
    {
        $data = $request->input();
        $posts = TopicPost::where('topic_id', $data['id'])->paginate(10);
        $actions = AUser::getPermissionAction(1232, 1);

        return view('topicpost.index')->with("lists", $posts)->with("actions", $actions)->with("topic_id", $data['id']);
    }


    public function getCreate(Request $request)
    {
        $data = $request->input();
        $topic_id = $data['topic_id'];

        return view('topicpost.create')->with('topic_id', $topic_id);
    }

    public function postCreate(TopicPostRequest $request)
    {
        $data = $request->input();

        unset($data['_token'], $data['formType'], $data['target_search']);

        TopicPost::create($data);
        if('project'==$data['type']){
            $project = Project::where('id', $data['post_id'])->first();
            $m_title = '恭喜您，您的项目已被无界投融选中为优质项目';
            $m_content = '亲爱的，您的项目被无界投融选为优质项目，并在无界投融专题页进行分享，点击发现-无界专题-十大项目查看。';

            MyMessage::createMessage($project->uid,$m_title,'official',$m_content,'','');
            $user=User::where('uid',$project->uid)->first();
            $content_sms=trans('sms.topicselectd');
            @SendSMS($user->username,$content_sms,'topicselectd',3);
        }
        return AjaxCallbackMessage('创建成功', true, url('topicpost/index?id='.$data['topic_id']));
    }


    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = TopicPost::where('id', $data['id'])->first();
        if('maker'==$list->type){
            $target = Maker::where('id', $list->post_id)->first();
            $return_data = array('id'=>$target->id, 'subject'=>$target->name);
        }elseif('project'==$list->type){
            $target = Project::where('id', $list->post_id)->first();
            $return_data = array('id'=>$target->id, 'subject'=>$target->subject);
        }elseif('investor'==$list->type){
            $target = User::where('uid', $list->post_id)->first();
            $return_data = array('id'=>$target->uid, 'subject'=>$target->username);
        }elseif('video'==$list->type){
            $target = ActivityProjectVideo::where('id', $list->post_id)->first();
            $return_data = array('id'=>$target->id, 'subject'=>$target->subject);
        }

        return view('topicpost.edit')->with('list', $list)->with('return_data', $return_data)->with('topic_id', $list->topic_id);
    }


    public function postEdit(TopicPostRequest $request)
    {
        $data = $request->input();
        $id = $data['topic_post_id'];
        unset($data['_token'], $data['formType'], $data['target_search'], $data['topic_post_id']);
        TopicPost::where('id', $id)->update($data);

        return AjaxCallbackMessage('编辑成功', true, url('topicpost/index?id='.$data['topic_id']));
    }

    public function postDelete(Request $request)
    {
        $data = $request->input();
        TopicPost::where('id', $data['pid'])->delete();
        TopicPostPhoto::where('topic_post_id', $data['pid'])->delete();

        return AjaxCallbackMessage('删除成功', true);
    }

}
